﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mPE.UI
{
    public partial class FrmPE_PN_AL_ModelRelease : Form
    {
        public FrmPE_PN_AL_ModelRelease()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String srcPath;//源路径
            string desPath = @"\\Tx-fs10\tx-fs1\LAMEX\Parametric\Module\For_NC";//目标路径

            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowNewFolderButton = false;
            dialog.Description = "请选择文件路径";
            dialog.SelectedPath = @"L:\Parametric\Sample";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                srcPath = dialog.SelectedPath;

                //1、判断是否只包含一级子目录，且只包含一个图号（一次只能发布一张图纸），否则提示并退出
                //TO DO
                //2、判断一级目录（图号_部件号）是否已发布，即检查图号是否出现在[NC_PN_AL_Detail_Part]表中,
                //   如果出现，则再检查发布时间是否为空；满足此两个条件，则提示用户是选择重新发布，还是直接退出
                //TO DO
                //3、重新发布前，删除当前图号相关信息，逻辑待定
                //TO DO

                //4、图纸发布（复制）子程序
                if (CopyFoldersWithSubDir(srcPath, desPath))
                {
                    MessageBox.Show("模型发布成功！");
                }
                else
                {
                    MessageBox.Show("模型发布失败！");
                }
            }
            this.Close();
        }

        /// <summary>
        /// 拷贝srcPath的文件到desPath下面
        /// </summary>
        /// <param name="srcPath">源路径(@"用户指定")</param>
        /// <param name="desPath">目标路径(@"\\Tx-fs10\tx-fs1\LAMEX\Parametric\Part")</param>
        /// <returns>返回:true-拷贝成功;false:拷贝失败</returns>
        public bool CopyFoldersWithSubDir(string srcPath, string desPath)
        {
            if (!Directory.Exists(desPath))
            {
                Directory.CreateDirectory(desPath);
            }
            #region //拷贝文件夹到desPath下
            try
            {
                string[] partDirs = Directory.GetDirectories(srcPath);//目录
                string[] partFiles = Directory.GetFiles(srcPath);//文件
                if (partFiles.Length > 0)
                {
                    for (int i = 0; i < partFiles.Length; i++)
                    {
                        if (Path.GetExtension(partFiles[i]) != ".xlsx")//排除.xlsx文件
                        {
                            File.Copy(srcPath + "\\" + Path.GetFileName(partFiles[i]), desPath + "\\" + Path.GetFileName(partFiles[i]), true);
                        }
                    }
                }
                if (partDirs.Length > 0)
                {
                    for (int j = 0; j < partDirs.Length; j++)
                    {
                        Directory.GetDirectories(srcPath + "\\" + Path.GetFileName(partDirs[j]));
                        //递归调用
                        CopyFoldersWithSubDir(srcPath + "\\" + Path.GetFileName(partDirs[j]), desPath + "\\" + Path.GetFileName(partDirs[j]));
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
            #endregion
            return true;
        }
    }
}
